package com.website.quartz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.website.common.vo.Result;
import com.website.mybatis.dao.QuartzDao;
import com.website.mybatis.entity.Quartz;
import com.website.quartz.mgr.QuartzTimerMgr;
import com.website.quartz.service.QuartzTimerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * <p>@author zhangshuw
 * <p>2018/7/27
 */
@Service
public class QuartzTimerServiceImpl implements QuartzTimerService {

    private static Logger logger = LoggerFactory.getLogger(QuartzTimerServiceImpl.class);

    @Autowired
    QuartzDao dao;

    @Autowired
    QuartzTimerMgr mgr;
    /**
     *
     * Description: 查询可启动的作业
     *
     * @return List<Quartz>
     * Create Date: 2013-11-2 下午1:46:04
     */
    @Override
    public List<Quartz> findJobs(){
        Quartz model = new Quartz();
        model.setState(Quartz.STATE_START);
        return dao.selectList(Wrappers.query(model));
    }
    @Override
    public IPage<Quartz> getPage(Quartz model, int page, int size) {
        return dao.selectPage(new Page<>(page, size), Wrappers.query(model));
    }
    @Override
    public Result add(Quartz model) {
        dao.insert(model);
        if(Quartz.STATE_START.equals(model.getState())){
            if(mgr.registerJobToScheduler(model)){
                model.setState(Quartz.STATE_PAUSE);
                return Result.error("保存成功，启动失败");
            }
        }
        return Result.success();
    }
    @Override
    public Result del(Long id) {
        Quartz model = dao.selectById(id);
        if(model != null){
            mgr.removeJob(model);
            dao.deleteById(id);
            return Result.success();
        }else{
            return Result.error("不存在该定时任务");
        }
    }
    @Override
    public Result updStart(Long id) {
        Quartz model = dao.selectById(id);
        if(model != null){
            if(mgr.registerJobToScheduler(model)){
                model.setState(Quartz.STATE_START);
                model.setModifyTime(null);
                dao.updateById(model);
                return Result.success();
            }
            return  Result.error("启动失败");
        }else{
            return Result.error("不存在该定时任务");
        }
    }
    @Override
    public Result updStop(Long id) {
        Quartz model = dao.selectById(id);
        if(model != null){
            if(mgr.removeJob(model)){
                model.setState(Quartz.STATE_PAUSE);
                model.setModifyTime(null);
                dao.updateById(model);
                return Result.success();
            }
            return Result.error("失败");
        }else{
            return Result.error("不存在该定时任务");
        }
    }
    @Override
    @Transactional
    public Result update(Quartz model) {
        if(model.getId() == null){
            return Result.error("id不能null");
        }
        Quartz v = dao.selectById(model.getId());
        if(v != null){
            mgr.removeJob(v);
            v.setState(model.getState());
            v.setJobName(model.getJobName());
            v.setCron(model.getCron());
            v.setJobClass(model.getJobClass());
            v.setJobDescribe(model.getJobDescribe());
            v.setOperator(model.getOperator());
            v.setPara(model.getPara());
            v.setModifyTime(null);
            if(Quartz.STATE_START.equals(model.getState())){
                dao.updateById(v);
                if(mgr.registerJobToScheduler(model)){
                    return Result.success("保存成功，任务启动成功");
                }
                return Result.success("保存成功，任务启动失败");
            }
            dao.updateById(v);
            return Result.success("保存成功");
        }else{
            return Result.error("id错误");
        }
    }
    @Override
    public QuartzTimerMgr getMgr() {
        return mgr;
    }
}
